var app = getApp(), u = app.require("utils/util.js"), a = app.require("utils/api.js"), o = app.require("utils/onfire.js"), h = app.require("utils/http.js"), n = app.require("utils/enums");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsNames: [
      { id: 0, name: '全 部' , type: 'all'},
      { id: 1, name: '待付款', type: 'unpay'},
      { id: 2, name: '待发货', type: 'unsend'},
      { id: 3, name: '待收货', type: 'send'},
      { id: 4, name: '待评价', type: 'un_comment'},
    ],
    currectIndex: 0,
    currectType:'all',
    emtyTxt: '暂无订单信息',
    orderList:[],
    orderPage:1,
    userInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var type = options.type ? options.type : 'all';
    this.data.userInfo = wx.getStorageSync('userInfo');
    this.getOrderList(type); 
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    this.getOrderList(this.data.currectType);  
  },
  // tab切换
  switchTab: function (e) {
    var t =this;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    t.data.orderPage = 1;
    t.data.orderList = [];
    t.getOrderList(type); 
    t.setData({
      currectIndex:index,
      currectType:type
    });
  },
  // 获取订单列表信息
  getOrderList: function (type){
    var t = this;
    var data = {
      store_id: a.storeId,
      uid: t.data.userInfo.uid,
      page: t.data.orderPage,
      type: type
    };
    h.postReq(a.orderListUrl, data, function (res) {
      if(res.code == 0){               
        t.setData({
          currectType:type,
          orderList: t.data.orderList.concat(res.data.order_list),
        });
        t.data.orderPage++; 
      }else{
        u.toast(res.message);
      }
    });
  },
  // 订单详情
  productInfo:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../index/product/info?id=' + id
    })
  },
  // 取消订单
  orderCancel:function(e){
    var t = this;
    var data = {
      order_no: e.currentTarget.dataset.order_no
    };
    h.noLoadReq(a.orderCancel, data, 'POST' ,function (res) {
      if(res.code == 0){        
        u.success('订单取消成功',function(){
          t.data.orderPage = 1,
          t.data.orderList = [],
          t.getOrderList(t.data.currectType);
        })      
      }else{
        u.toast(res.message);
      }
    });
  },
  // 去付款
  toPay:function(e){
    var order_no = e.currentTarget.dataset.order_no;
    wx.navigateTo({
      url: '/pages/order/pay/index?order_no=' + order_no,
    })
  },
  // 确认收货
  orderReceive:function(e){
    var t = this;
    var data = {
      order_no: e.currentTarget.dataset.order_no
    };
    u.confirm('是否确认收货？', function () {
      h.noLoadReq(a.orderReceive, data, 'POST', function (res) {
        if (res.code == 0) {
          u.success('确认收货成功', function () {
            t.data.orderPage = 1,
              t.data.orderList = [],
              t.getOrderList(t.data.currectType);
          })
        } else {
          u.toast(res.message);
        }
      });
    });    
  },
  // 去评价
  toEvaluate:function(e){
    var order_no = e.currentTarget.dataset.order_no;
    wx.navigateTo({
      url: '/pages/mine/evaluate/to_evaluate?order_no=' + order_no,
    })
  },

})